from xml.etree.ElementTree import ElementTree, dump, fromstring, Element, tostring


#	<transfer id="" type="">
#		<source>
#			<svc name="" type="">
#				<smb_access>
#					<share></share>
#					<path></path>
#				</smb_access>
#				<local_access>
#					<path></path>
#				<local_access>
#				<ftp_access>
#					<user></user>
#					<password></password>
#					<passive></passive>
#					<port></port>
#					<path></path>
#				</ftp_access>
#				<host></host>
#			</svc>
#			<object type="">
#				<file>
#					<filename></filename>
#					<path></path>
#					<size></size>
#				</file>
#
#				<media>
#				</media>
#			</object>
#		</source>
#		<transfer_action type="Transcode">
#			<transcode engine="Carbon">
#				<carbon type="Segment|Stich|AvidIntegration|None">
#					<guid></guid>
#					<ext></ext>
#					<project></project>
#					<segment>
#						<input></input>
#						<output></output>
#						<basename></basename>
#					</segment>
#				</carbon>
#			</transcode>
#		</transfer_action>
#		<destination>
#			<svc name="" type="">
#				<smb_access>
#					<share></share>
#					<path></path>
#				</smb_access>
#				<local_access>
#					<path></path>
#				<local_access>
#				<ftp_access>
#					<user></user>
#					<password></password>
#					<passive></passive>
#					<port></port>
#					<path></path>
#				</ftp_access>
#				<host></host>
#			</svc>
#		</destination>
#	</transfer>


class TransferXmlError(Exception):
    def __init__(self, error = 0):
	self.error = error
	
    def toString(self):
	if self.error == 1:
	    return "TransLocalAccess.toElement() Error: Element(""path"") return None"
	if self.error == 2:
	    return "TransLocalAccess.toElement() Error: Element(""local_access"") return None"
	if self.error == 3:
	    return "TransLocalAccess.fromElement() Error: path not found in element"
	

class TransLocalAccess(object):
#	<local_access>
#		<path></path>
#	</local_access>
    def __init__(self, element = None):
	self.path = ''

	if element is not None:
	    self.fromElement(element)
	
    def fromElement(self, element):
	path = element.find("path")
	if path is not None:
	    self.path = path.text
	else:
	    raise TransferXmlError(3)
    
    def toElement(self):
	element = Element("local_access")
	if element is not None:
	    path    = Element("path")
	    if path is not None:
		path.text = self.path
		element.append(path)
		return element
	    else:
		raise TransferXmlError(1)
	else:
	    raise TransferXmlError(2)



class TransSmbAccess(object):
#	<smb_access>
#		<share></share>
#		<path></path>
#	</smb_access>
    def __init__(self, element = None):
	self.share = ''
	self.path  = ''
	
	if element is not None:
	    self.fromElement(element)
	    
	    
    def fromElement(self, element):
	path = element.find("path")
	if path is not None:
	    share = element.find("share")
	    if share is not None:
		self.share = share.text
		self.path  = share.text
	    else:
		raise TransferXmlError(7)
	else:
	    raise TransferXmlError(8)
	    

    def toElement(self):
	element = Element("smb_access")
	if element is not None:
	    
	    share=Element("share")
	    if share is not None:
	    
		path=Element("path")
		if path is not None:
		    share.text =self.share
		    path.text  =self.path
	
		    element.append(share)
		    element.append(path)
	
		    return element
		else:
		    raise TransferXmlError(4)
	    else:
		raise TransferXmlError(5)
	else:
	    raise TransferXmlError(6)



class TransFtpAccess(object):
#	<ftp_access>
#		<user></user>
#		<password></password>
#		<path></path>
#		<passive></passive>
#		<port></port>
#	</ftp_access>
    def __init__(self, element = None):
	self.user=None
	self.password=None
	self.path=None
	self.passive=None
	self.port=None

	if element is not None:
	    self.fromElement(element)

    def fromElement(self, element):
	user = element.find("user")
	if user is None:
	    raise TransferXmlError(9)
	
	password = element.find("password")
	if password is None:
	    raise TransferXmlError(10)
	    
	path = element.find("path")
	if path is None:
	    raise TransferXmlError(11)
	
	passive = element.find("passive")
	if passive is None:
	    raise TransferXmlError(12)
	
	port = element.find("port")
	if port is None:
	    raise TransferXmlError(13)
	    
	self.user     = user.text
	self.passowrd = password.text
	self.path     = path.text
	self.passive  = passive.text
	self.port     = port.text



    def toElement(self):
	element = Element("ftp_access")
	
	user     = Element("user")
	password = Element("password")
	path     = Element("path")
	passive  = Element("passive")
	port     = Element("port")
	
	if user is None:
	    raise TransferXmlError(14)
	
	if password is None:
	    raise TransferXmlError(15)
	    
	if path is None:
	    raise TransferXmlError(16)
	    
	if passive is None:
	    raise TransferXmlError(17)
	
	if port is None:
	    raise TransferXmlError(18)
	
	
	user.text     = self.user
	password.text = self.password
	path.text     = self.path
	passive.text  = self.passive
	port.text     = self.port
	
	element.append(user)
	element.append(password)
	element.append(path)
	element.append(passive)
	element.append(port)
	
	return element


class TransSvc(object):
#	<svc name="" type"">
#		<host></host>
#		<smb_access>
#		</smb_access>
#		<ftp_access>
#		</ftp_access>
#		<local_access>
#		</local_access>
#	</svc>
    def __init__(self, element = None):
	self.name=None
	self.type=None
	self.host=None
	self.smb_access=None
	self.ftp_access=None
	self.local_access=None
	
	if element is not None:
	    self.fromElement(element)

    def fromElement(self, element):
	
	name = element.attrib["name"]
	type = element.attrib["type"]
	
	if type is None:
	    raise XmlTransferError(19)
	
	if name is None:
	    raise XmlTransferError(20)
	
	if type == "Server":
	    self.name = name
	    self.type = type
	    
	    host = element.find("host")
	    if host is None:
		raise XmlTransferError(21)
	    
	    self.host = host.text
	    
	    smb_access = element.find("smb_access")
	    if smb_access is None:
		raise XmlTransferError(22)
		
	    ftp_access = element.find("ftp_access")
	    if ftp_access is None:
		raise XmlTransferError(23)
		
	    local_access = element.find("local_access")
	    if local_access is None:
		raise XmlTransferError(24)
	
	    self.smb_access = TransSmbAccess(smb_access)
	    self.ftp_access = TransFtpAccess(ftp_access)
	    self.local_access = TransLocalAccess(local_access)
	    

    def toElement(self):
	element = Element("svc")
	element.attrib["name"] = self.name
	element.attrib["type"] = self.type
	
	if self.type == "Server":
	    element.append(self.smb_access.toElement())
	    element.append(self.ftp_access.toElement())
	    element.append(self.local_access.toElement())
	    
	    host = Element("host")
	    if host is None:
		raise XmlTransferError(25)
	
	    host.text = self.host
	    element.append(host)	
	    
	return element


class TransFile(object):
#	<file>
#		<filename></filename>
#		<path></path>
#		<size></size>
#	</file>
    def __init__(self, element=None):
	self.filename = None
	self.path     = None
	self.size     = None
	
	if element is not None:
	    self.fromElement(element)
	    
    def fromElement(self, element):
	filename = element.find("filename")
	if filename is not None:
	    self.filename = filename.text
	else:
	    raise XmlTransferError(28)
	    
	path = element.find("path")
	if path is not None:
	    self.path = path.text
	else:
	    raise XmlTransferError(26)
	
	size = element.find("size")
	if size is not None:
	    self.size = size.text
	else:
	    raise XmlTransferError(27)
    
    def toElement(self):
	element = Element("file")
	if element is not None:
	    file = Element("filename")
	    path = Element("path")
	    size = Element("size")

	    if file is None:
		raise XmlTransferError(28)
		
	    if path is None:
		raise XmlTransferError(29)
		
	    if size is None:
		raise XmlTransferError(30)
		
	    
	    file.text = self.filename
	    path.text = self.path
	    size.text = self.size
	    
	    element.append(file)
	    element.append(path)
	    element.append(size)
	
	    return element
	



class TransSource(object):
#	<source>
#		<svc name="" type="">
#		</svc>
#		<object type"">
#		</object>
#	</source>
    def __init__(self, element = None):
	self.svc     = None
	self.object  = None
	
	if element is not None:
	    self.fromElement(element)
	
    def fromElement(self, element):
	svc = element.find("svc")
	if svc is None:
	    raise XmlTransferError(26)
	    
	object = element.find("object")
	if object is None:
	    raise XmlTransferError(27)
	
	self.object = TransObject(object)
	
	self.svc = TransSvc(svc)
	
    def toElement(self):
	element = Element("source")
	if element is None:
	    raise XmlTransferError(31)
	
	element.append(self.svc.toElement())
	element.append(self.object.toElement())
	
	return element


class TransDestination(object):
#	<destination>
#		<svc name="" type="">
#		</svc>
#	</destination>
    def __init__(self, element = None):
	self.svc     = None
	
	if element is not None:
	    self.fromElement(element)
	
    def fromElement(self, element):
	svc = element.find("svc")
	if svc is None:
	    raise XmlTransferError(26)
	    
	self.svc = TransSvc(svc)
	
    def toElement(self):
	element = Element("destination")
	if element is None:
	    raise XmlTransferError(31)
	
	element.append(self.svc.toElement())
	return element


def NewTransfer(transferType,  sourceSvcType, sourceObjectType, transferActionType, destinationSvcType):
    transfer = Transfer()
    
    transfer.id = ''
    transfer.type = transferType
    transfer.source          = TransSource()
    transfer.transfer_action = TransferAction()
    transfer.destination     = TransDestination()

    transfer.source.svc      = TransSvc()
    transfer.source.object   = TransObject()

    transfer.source.svc.type    = sourceSvcType
    
    transfer.source.object.type = sourceObjectType
    if sourceObjectType == "File":
	transfer.source.object.object = TransFile()
    else:
	if sourceObjectType == "Media":
	    transfer.source.object.object = TransMedia()

    transfer.transfer_action.type = transferActionType
    
    if transferActionType == "Transcode":
	transfer.transfer_action.action = Transcode()
    else:
	if transferActionType == "None":
	    transfer.transfer_action.action = None	
    
    transfer.destination.svc      = TransSvc()
    transfer.destination.svc.type = destinationSvcType


class TransObject(object):
#	<object type="">
#		<file|media>
#		</file|media>
#	</object>

    def __init__(self, element = None):
	self.type = None
	self.object = None

	if element is not None:
	    self.fromElement(element)
	    
    def fromElement(self, element):
	type = element.attrib["type"]
	if type is not None:
	    if type == "File":
		self.type = "File"
		self.object = TransFile(element.find("file"))
	    else: 
		if type == "Media":
		    self.type = "Media"
		    self.object = TransMedia(element.find("media"))
	else:
	    raise XmlTransferError(32)
	
    def toElement(self):
	element = Element("object")
	
	
	if element is None:
	    raise XmlTransferError(33)
	
	element.attrib["type"] = self.type
	element.append(self.object.toElement())
	
	
	
	return element



class Transfer(object):
    def __init__(self, XMLString = None):
	self.id     = None
	self.type   = None
	self.source = None
	self.transfer_action = None
	self.destination = None
	
	if XMLString is not None:
	    self.fromXMLString(XMLString)
	else:
	    self.source = TransSource()
	    self.transfer_action = TransferAction()
	    self.destination = TransDestination()
	
	

    def fromXMLString(self, XMLString):
	element = fromstring(XMLString)
	if element is not None:
	    self.id = element.attrib["id"]
	    self.type = element.attrib["type"]
	    
	    source = element.find("source")
	    if source is not None:
		self.source = TransSource(source)
	    
	    transfer_action = element.find("transfer_action")
	    if transfer_action is not None:
		self.transfer_action = TransferAction(transfer_action)
	    
	    destination = element.find("destination")
	    if destination is not None:
		self.destination = TransDestination(destination)
	    

    def toXMLString(self):
	element = Element("transfer")
	element.attrib["id"] = self.id
	element.attrib["type"] = self.type
	
	element.append(self.source.toElement())
	element.append(self.transfer_action.toElement())
	element.append(self.destination.toElement())
	
	string = tostring(element)
	return string



class TransSegment(object):
    def __init__(self, element=None):
    	self.input  = None
	self.output = None
	self.basename = None
	
	if element is not None:
	    self.fromElement(element)
	else:
	    self.input = ''
	    self.output = ''
	    self.basename = ''
	    

    def fromElement(self, element):
	input = element.find("input")
	if input is not None:
	    self.input = input.text
	
	output = element.find("output")
	if output is not None:
	    self.output = output.text
	    
	basename = element.find("basename")
	if basename is not None:
	    self.basename = basename.text

	
    def toElement(self):
	element = Element("Segment")
	input	= Element("in")
	output	= Element("out")
	basename = Element("basename")
	
	input.text = self.input
	output.text = self.output
	basename.text = self.basename
	
	element.append(input)
	element.append(output)
	element.append(basename)
	
	return element
	
	
class TransferAction(object):
    def __init__(self, element=None):
	self.type = None
	self.action = None

	if element is not None:
	    self.fromElement(element)


    def fromElement(self, element):
	type = element.attrib["type"]
	if type is not None:
	    self.type = type
	    if type == "Transcode":
		self.action = Transcode(element.find("transcode"))


    def toElement(self):
	element = Element("transfer_action")
	if self.type is not None:
	    element.attrib["type"] = self.type
	else:
	    element.attrib["type"] = ''
	if self.action is not None:
	    element.append(self.action.toElement())
	
	return element

    
	
class Transcode(object):
    def __init__(self, element=None):
	self.engine = None
	self.carbon = None
	
	if element is not None:
	    self.fromElement(element)

    def fromElement(self, element):
	engine = element.attrib["engine"]
	if engine is not None:
	    self.engine = engine
	    if engine == "Carbon":
		self.carbon = TransCarbon(element.find("carbon"))


    def toElement(self):
	element = Element("trancode")
	element.attrib["engine"] = self.engine
	
	element.append(self.carbon.toElement())
	
	return element
	

class TransCarbon(object):
    def __init__(self, element=None):
	self.type    = None
	self.project = None
	self.guid    = None
	self.ext     = None
	self.segment = []
	
	if element is not None:
	    self.fromElement(element)

    def fromElement(element):
	type = element.attrib["type"]
	if type is not None:
	    self.type =	type
	
	    if type == "Segment" or type == "AvidIntegration" or type == "Stich":
		segment = element.findall("segment")
		for s in segment:
		    self.segment.append(TransSegment(s))
	    
	project = element.find("project")
	if project is not None:
	    self.project = project.text
	
	guid    = element.find("guid")
	if guid is not None:
	    self.guid = guid.text
	    
	ext     = element.find("ext")
	if ext is not None:
	    self.ext = ext.text
	


	
    def toElement(self):
	element = Element("carbon")
	element.attrib["type"] = self.type

	project = Element("project")
	guid    = Element("guid")
	ext     = Element("ext")
	
	project.text = self.project
	guid.text    = self.guid
	ext.text     = self.ext
	
	element.append(project)
	element.append(guid)
	element.append(ext)
	
	for s in segment:
	    element.append(s.toElement())
	    
	return element
	



class TransMedia(object):
    def __init__(self, element=None):
	self.filename = None
	self.path     = None
	self.size     = None
	self.fps	    = None
	self.start_tc	    = None
	self.format	    = None
	self.video	    = None
	self.bitrate	    = None
	self.audio_channels = None
	self.wrapper        = None
	
	if element is not None:
	    self.fromElement(element)
	
    def fromElement(self, element):
	file       = element.find("filename")
	size       = element.find("size")
	path       = element.find("path")
	fsp        = element.find("fps")
	starttc    = element.find("start_tc")
	format     = element.find("format")
	video      = element.find("video")
	bitrate    = element.find("bitrate")
	a_channels = element.find("audio_channels")
	wrapper    = element.find("wrapper")


	if file is None:
	    raise XmlTransferError(34)
	if size is None:
	    raise XmlTransferError(35)
	if path is None:
	    raise XmlTransferError(36)
	if fsp is None:
	    raise XmlTransferError(37)
	if starttc is None:
	    raise XmlTransferError(38)
	if format is None:
	    raise XmlTransferError(39)
	if video is None:
	    raise XmlTransferError(40)
	if bitrate is None:
	    raise XmlTransferError(41)
	if a_channels is None:
	    raise XmlTransferError(42)
	if wrapper is None:
	    raise XmlTransferError(43)
	    
	self.filename = file.text
	self.path     = path.text
	self.size     = size.text
	self.fps	    = fps.text
	self.start_tc	    = starttc.text
	self.format	    = format.text
	self.video	    = video.text
	self.bitrate	    = bitrate.text
	self.audio_channels = a_channels.text
	self.wrapper        = wrapper.text


    def toElement(self):
	element = Element("media")
	if element is not None:
	    file       = Element("filename")
	    size       = Element("size")
	    path       = Element("path")
	    fsp        = Element("fps")
	    starttc    = Element("start_tc")
	    format     = Element("format")
	    video      = Element("video")
	    bitrate    = Element("bitrate")
	    a_channels = Element("audio_channels")
	    wrapper    = Element("wrapper")
	
	    if file is None:
		raise XmlTransferError(44)
	    if size is None:
		raise XmlTransferError(45)
	    if path is None:
	        raise XmlTransferError(46)
	    if fsp is None:
	        raise XmlTransferError(47)
	    if starttc is None:
	        raise XmlTransferError(48)
	    if format is None:
	        raise XmlTransferError(49)
	    if video is None:
	        raise XmlTransferError(50)
	    if bitrate is None:
	        raise XmlTransferError(51)
	    if a_channels is None:
	        raise XmlTransferError(52)
	    if wrapper is None:
	        raise XmlTransferError(53)
	    
	
	
	    file.text       = self.filename
	    size.text       = self.size
	    path.text       = self.path
	    fps.text        = self.fps
	    starttc.text    = self.start_tc
	    format.text     = self.format
	    video.text      = self.video
	    bitrate.text    = self.bitrate
	    a_channels.text = self.audio_channels
	    wrapper.text    = self.wrapper
	    
	    element.append(file)
	    element.append(size)
	    element.append(path)
	    element.append(fps)
	    element.append(starttc)
	    element.append(format)
	    element.append(video)
	    element.append(bitrate)
	    element.append(a_channels)
	    element.append(wrapper)
	    
	    return element
	
	













